package com.java.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import com.java.dao.PatientDao;
import com.java.entity.Patient;
import com.java.ext.MyButton;
import com.java.ext.MyJTable;
import com.java.model.PatientTableModel;

/**
* @ClassName: DoctorMainWindow
* @Description: 医生登陆成功主界面
* @author ChenJiaLu
* @date 2023年12月29日 下午2:57:09
*
 */
public class DoctorMainWindow extends BaseJFrame {
	private static final long serialVersionUID = 1L;

	private List<Patient> Patients = new ArrayList<Patient>();

	private Patient selectObj = null;

	private JTable table = null;

	public DoctorMainWindow() {
		setTitle("医院就诊管理系统——医生系统");
		setSize(700, 480);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		initMenu(this);

		PatientDao dDao = new PatientDao();
		Patients = dDao.getPatients();

		PatientTableModel md = new PatientTableModel(Patients);
		table = new MyJTable(md);
		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, BorderLayout.CENTER);

		// 创建输入框和按钮
		JPanel inputPanel = new JPanel();
		inputPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		final MyButton updateButton = new MyButton("修改").warning();
		final MyButton deleteButton = new MyButton("删除").danger();
		inputPanel.add(updateButton);
		inputPanel.add(deleteButton);
		add(inputPanel, BorderLayout.SOUTH);
		updateButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (null == selectObj) {
					error("请选中");
					return;
				}
				new AddPatientWindow(bJFrame, selectObj);
			}
		});
		deleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (null == selectObj) {
					error("请选中");
					return;
				}
				if (confirm("是否删除患者【" + selectObj.getName() + "】")) {
					PatientDao dDao = new PatientDao();
					dDao.delPatient(selectObj.getId());
					dDao.close();
					PatientDao sDao = new PatientDao();
					Patients = sDao.getPatients();
					table.setModel(new PatientTableModel(Patients));
				}
			}
		});

		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 1) {
					int row = table.rowAtPoint(e.getPoint());
					selectObj = getObj(row);
					updateButton.setEnabled(true);
					deleteButton.setEnabled(true);
				}
				if (e.getClickCount() == 2) {
					setEnabled(false);
					new AddPatientWindow(bJFrame, selectObj);
				}
			}
		});

		setVisible(true);
	}

	public Patient getObj(int index) {
		for (int i = 0; i < Patients.size(); i++) {
			if (index == i) {
				return Patients.get(i);
			}
		}
		return null;
	}

}